package com.ofc.chart;

import java.util.LinkedList;
import java.util.Random;

import com.ofc.chart.element.Element;
import com.ofc.chart.element.Style;
import com.ofc.chart.element.Title;
import com.ofc.chart.element.XAxis;
import com.ofc.chart.element.YAxis;
import com.ofc.chart.element.YLegend;
import com.ofc.chart.model.Bar3D;
import com.ofc.chart.model.Line;
import com.ofc.chart.model.Pie;

/**
 * Chart Json 构建模版类
 * 
 * @author zhangpeng
 * @date 2012-12-6
 */
public class ChartJsonTemplate {
	private LinkedList<String> colours = new LinkedList<String>();

	public ChartJsonTemplate() {
		colours.add("#8f0e73");// 紫色
		colours.add("#0f5390");// 深蓝
		colours.add("#788e08");// 绿色
		colours.add("#ff0000");// 红色
		colours.add("#ffa800");// 金黄
		colours.add("#09e7ce");// 蔚蓝
	}

	public Bar3D getBar3DTemplate(String xTitle, String yTitle,
			LinkedList<String> labels, Double maxValue,
			LinkedList<String> elementText,
			LinkedList<LinkedList<String>> values) {
		if (elementText == null || values == null) {
			return null;
		}
		if (elementText.size() != values.size()) {
			System.out.println("标题和值的数量不相同，无法生成柱状图！");
			return null;
		}

		if (colours.size() < elementText.size()) {
			int count = elementText.size() - colours.size();
			for (int i = 0; i < count; i++) {
				String hexNum = getRandHexNumer();
				colours.add("#" + hexNum);
			}
		}

		Title title = new Title();
		Style style = new Style();
		style.setFontSize("20px");
		style.setColor("#0000ff");
		style.setFontFamily("Verdana");
		style.setTextAlign("center");
		title.setText(xTitle);//
		title.setStyle(style);

		YLegend ylegend = new YLegend();
		Style style1 = new Style();
		style1.setFontSize("12px");
		style1.setColor("#736AFF");
		style1.setPadding("10px");
		ylegend.setText(yTitle);//
		ylegend.setStyle(style1);

		XAxis xaxis = new XAxis();
		xaxis.setStroke(1);
		xaxis.setTickHeight(10);
		xaxis.setColour("#909090");
		xaxis.setGridColour("#ADB5C7");
		xaxis.setLabels(labels);//
		xaxis.set3d(10);

		YAxis yaxis = new YAxis();
		yaxis.setStroke(4);
		yaxis.setTickHeight(3);
		yaxis.setColour("#909090");
		yaxis.setGridColour("#ADB5C7");
		yaxis.setOffset(0);
		yaxis.setMax(maxValue);//

		LinkedList<Element> elements = new LinkedList<Element>();
		int count = elementText.size();
		for (int i = 0; i < count; i++) {
			Element element = new Element();
			element.setType("bar_3d");
			element.setAlpha(new Float(0.8));
			LinkedList<String> colourList = new LinkedList<String>();
			colourList.add(colours.get(i));
			element.setColours(colourList);
			element.setText(elementText.get(i));//
			element.setFontSize(12);
			element.setValues(values.get(i));//
			elements.add(element);
		}

		Bar3D bar3D = new Bar3D();
		bar3D.setTitle(title);
		bar3D.setYlegend(ylegend);
		bar3D.setXaxis(xaxis);
		bar3D.setYaxis(yaxis);
		bar3D.setElements(elements);

		return bar3D;
	}

	public Pie getPieTemplate(String xTitle, LinkedList<String> values) {
		if (colours.size() < values.size()) {
			int count = values.size() - colours.size();
			for (int i = 0; i < count; i++) {
				String hexNum = getRandHexNumer();
				colours.add("#" + hexNum);
			}
		}

		Title title = new Title();
		Style style = new Style();
		style.setFontSize("20px");
		style.setColor("#0000ff");
		style.setFontFamily("Verdana");
		style.setTextAlign("center");
		title.setText(xTitle);//
		title.setStyle(style);

		LinkedList<Element> elements = new LinkedList<Element>();
		Element element = new Element();
		element.setType("pie");
		element.setAlpha(new Float(0.8));
		element.setAnimate("1");
		element.setStartAngle(180);
		element.setStroke(2);
		element.setColours(colours);
		element.setValues(values);//
		// element.setTip("#val# of #total# #percent# of 100%");//
		element.setTip("#label#,#percent#");//
		elements.add(element);

		XAxis xaxis = new XAxis();

		Pie pie = new Pie();
		pie.setTitle(title);
		pie.setXaxis(xaxis);
		pie.setElements(elements);
		return pie;
	}

	public Line getLineTemplate(String xTitle, String yTitle,
			LinkedList<String> labels, Double maxValue,
			LinkedList<String> elementText,
			LinkedList<LinkedList<String>> values) {
		if (elementText == null || values == null) {
			return null;
		}
		if (elementText.size() != values.size()) {
			return null;
		}

		if (colours.size() < elementText.size()) {
			int count = elementText.size() - colours.size();
			for (int i = 0; i < count; i++) {
				String hexNum = getRandHexNumer();
				colours.add("#" + hexNum);
			}
		}

		Title title = new Title();
		Style style = new Style();
		style.setFontSize("20px");
		style.setColor("#0000ff");
		style.setFontFamily("Verdana");
		style.setTextAlign("center");
		title.setText(xTitle);//
		title.setStyle(style);

		YLegend ylegend = new YLegend();
		Style style1 = new Style();
		style1.setFontSize("12px");
		style1.setColor("#736AFF");
		ylegend.setText(yTitle);//
		ylegend.setStyle(style1);

		XAxis xaxis = new XAxis();
		xaxis.setLabels(labels);//

		YAxis yaxis = new YAxis();
		yaxis.setMax(maxValue);//

		LinkedList<Element> elements = new LinkedList<Element>();
		int count = elementText.size();
		for (int i = 0; i < count; i++) {
			Element element = new Element();
			element.setType("line");
			LinkedList<String> colourList = new LinkedList<String>();
			colourList.add(colours.get(i));
			element.setColours(colourList);
			element.setText(elementText.get(i));//
			element.setFontSize(12);
			element.setValues(values.get(i));//
			elements.add(element);
		}

		Line line = new Line();
		line.setTitle(title);
		line.setYlegend(ylegend);
		line.setXaxis(xaxis);
		line.setYaxis(yaxis);
		line.setElements(elements);
		return line;
	}

	public String getRandHexNumer() {
		String hexNum = Integer.toHexString(new Random().nextInt(999999));
		int len = hexNum.length();
		if (len == 1) {
			hexNum = "00000" + hexNum;
		} else if (len == 2) {
			hexNum = "0000" + hexNum;
		} else if (len == 3) {
			hexNum = "000" + hexNum;
		} else if (len == 4) {
			hexNum = "00" + hexNum;
		} else if (len == 5) {
			hexNum = "0" + hexNum;
		}
		return hexNum;
	}

}
